package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.BusinessEnum;
import com.powernode.constant.StoreConstants;
import com.powernode.domain.IndexImg;
import com.powernode.domain.Prod;
import com.powernode.ex.handler.BusinessException;
import com.powernode.feign.StoreProdFeign;
import com.powernode.mapper.IndexImgMapper;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author 贺
* @description 针对表【index_img(主页轮播图)】的数据库操作Service实现
* @createDate 2024-07-19 13:40:40
*/
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.IndexImgServiceImpl")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg>
    implements IndexImgService{

    @Resource
    private IndexImgMapper indexImgMapper;
    @Resource
    private StoreProdFeign storeProdFeign;

    /**
     * 新增轮播图
     *
     * @param indexImg
     * @return
     */
    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG_LIST_KEY)
    public Boolean saveIndexImg(IndexImg indexImg) {
        // 获取轮播图类型
        Integer type = indexImg.getType();
        // 补充轮播图对象属性
        indexImg.setShopId(1L);
        indexImg.setCreateTime(new Date());
        // 判断关联类型
        if (-1 == type) {
            // 未关联商品
            indexImg.setProdId(null);
        }
        return indexImgMapper.insert(indexImg) > 0;
    }

    /**
     * 根据ID查询轮播图详情
     *
     * @param indexImgId
     * @return
     */
    @Override
    public IndexImg queryIndexImgInfoById(Long indexImgId) {
        // 查询轮播图详情
        IndexImg indexImg = indexImgMapper.selectById(indexImgId);
        // 判断轮播图是否关联商品
        Integer type = indexImg.getType();
        if (0 == type) {
            // 当前轮播图已关联商品
            // 获取轮播图关联商品的ID
            Long prodId = indexImg.getProdId();
            // 远程调用：根据商品ID查询商品详情
            Result<List<Prod>> result = storeProdFeign.getProdListByIds(Collections.singletonList(prodId));
            // 判断远程调用结果
            if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())) {
                // 远程调用失败
                throw new BusinessException(result.getMsg());
            }
            // 获取商品对象集合
            List<Prod> prodList = result.getData();
            // 判断商品对象集合是否为空
            if (CollectionUtil.isNotEmpty(prodList) && prodList.size() > 0) {
                // 获取商品对象
                Prod prod = prodList.get(0);
                // 设置轮播图关联商品的图片和名称
                indexImg.setPic(prod.getPic());
                indexImg.setProdName(prod.getProdName());
            }
        }

        return indexImg;
    }

    /**
     * 修改轮播图
     *
     * @param indexImg
     * @return
     */
    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG_LIST_KEY)
    public Boolean modifyIndexImg(IndexImg indexImg) {
        return indexImgMapper.updateById(indexImg) > 0;
    }

    /**
     * 批量删除轮播图
     *
     * @param indexImgIds
     * @return
     */
    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG_LIST_KEY)
    public Boolean removeIndexImg(List<Long> indexImgIds) {
        return indexImgMapper.deleteBatchIds(indexImgIds) > 0;
    }

    /**
     * 查询小程序轮播图列表
     *
     * @return
     */
    @Override
    @Cacheable(key = StoreConstants.WX_INDEX_IMG_LIST_KEY)
    public List<IndexImg> queryIndexImgList() {
        return indexImgMapper.selectList(new LambdaQueryWrapper<IndexImg>()
                .eq(IndexImg::getStatus, 1)
                .orderByDesc(IndexImg::getSeq));
    }
}




